<?php
// 会员信息

namespace app\shop\controller;

use think\Cache;
use think\Request;
use think\Session;
use think\Exception;
use think\Controller;
use app\shop\model\Member as MemberModel;
use app\shop\model\MemberAddress as MemberAddressModel;

class Member extends Controller
{
    /**
     * 绑定手机号
     * @param Request $request
     * @param MemberModel $member
     * @return array|mixed
     */
    public function bindMobile(Request $request,MemberModel $member)
    {
        // 获取用户ID
        $memberId = Session::get('member_id');

        if ($request->isPost()) {
            // 获取参数
            $username = $request->param('username');
            $code = $request->param('codes');

            // 验证码判断
            $trueCode = Cache::get('bind_mobile' . $username);
            if ($code != $trueCode) return ['code' => 401, 'message' => '验证码错误'];

            // 绑定手机号
            $operation = $member->allowField(true)->save(['username' => $username], ['id' => $memberId]);

            if ($operation) {
                return ['code' => 200];
            }
            return ['code' => 401, 'message' => '系统服务器繁忙！稍后再试！'];
        }

        return $this->fetch('',[
            'mate_head'=> '绑定手机号'
        ]);
    }


    /**
     * 修改绑定手机号
     * @param Request $request
     * @param MemberModel $members
     * @return array|mixed
     */
    public function modifyMobile(Request $request,MemberModel $members)
    {
        // 获取用户ID
        $memberId = Session::get('member_id');

        if ($request->isPost()) {
            // 获取参数
            $old_username = $request->param('old_username'); //旧手机号
            $username = $request->param('username'); //新手机号
            $code = $request->param('codes');  // 验证码

            // 获取用户信息
            $member = $members::get($memberId);
            // 验证会员信息
            if ($member['username'] != $old_username) return ['code' => 401, 'message' => '原手机号不匹配'];
            // 验证验证码
            $trueCode = Cache::get('modify_mobile' . $username);
            if ($code != $trueCode) return ['code' => 401, 'message' => '验证码错误'];

            // 修改手机号
            $operation = $members->allowField(true)->save(['username' => $username]);


            if ($operation) {
                return ['code' => 200];
            }
            return ['code' => 401, 'message' => '系统服务器繁忙！稍后再试！'];

        }


        return $this->fetch('', [
            'mate_head' => '重置手机号码'
        ]);
    }


    /**
     * 修改支付密码
     * @param Request $request
     * @param MemberModel $members
     * @return array|mixed
     */
    public function modifyPassword(Request $request,MemberModel $members)
    {
        // 获取用户ID
        $memberId = Session::get('member_id');

        if ($request->isPost()) {
            // 获取数据
            $oldPassword = $request->param('old_paypass', '', 'mcs');

            $newPassword = $request->param('pay_pass', '', 'mcs');

            if ($oldPassword == $newPassword) return ['code' => 402, 'message' => '新密码和旧密码不能一致！'];
            // 获取用户信息
            $member = $members::get($memberId);

            // 判断旧密码是否正确
            if ($oldPassword == $member['pay_pass']) {
                $operation = $members->allowField(true)->save(['pay_pass' => $newPassword]);
                if ($operation) {
                    return ['code' => 200];
                }

                return ['code' => 401, 'message' => '系统服务器繁忙！稍后再试！'];
            }

            return ['code' => 402, 'message' => '原密码输入错误'];

        }
        return $this->fetch('', [
            'mate_head' => '重置密码'
        ]);
    }


    /**
     * 设置支付密码
     * @param Request $request
     * @param MemberModel $member
     * @return array|mixed
     */
    public function setPassword(Request $request,MemberModel $member)
    {
        // 获取用户ID
        $memberId = Session::get('member_id');

        if ($request->isPost()) {
            // 获取数据
            $pay_pass = $request->param('pay_pass', '', 'mcs');

            // 设置支付密码
            $operation = $member->allowField(true)->save(['pay_pass' => $pay_pass], ['id' => $memberId]);

            if ($operation) {
                return ['code' => 200];
            }

            return ['code' => 401, 'message' => '系统服务器繁忙！稍后再试！'];
        }


        return $this->fetch('', [
            'mate_head' => '设置支付密码'
        ]);
    }


    /**
     * 修改个人信息
     * @param Request $request
     * @param MemberModel $members
     * @return array
     * @throws Exception
     */
    public function set_persional(Request $request,MemberModel $members)
    {
        // 获取用户ID
        $memberId = Session::get('member_id');

        if ($request->isPost()) {

            // 抛出异常
            if (empty($memberId)) throw new Exception('not Empty');

            // 获取参数
            $data = $request->param();

            //验证昵称长度
            // $validate = Loader::validate('Member');
            // if (!$validate->sence('nick')->check($data)) return ['code'=>401 ,'message'=>$validate->getError()];

            // 设置个人信息
            $operation = $members->allowField(true)->save($data, ['id' => $memberId]);

            if ($operation||$operation==0) {
                Session::set('member_name', $data['nickname']);
                return ['code' => 200];
            }

            return ['code' => 401, 'message' => '系统服务器繁忙！稍后再试！'];
        }
    }


    /**
     * 设置收货地址
     * @param Request $request
     * @param MemberAddressModel $members
     * @return array
     * @throws Exception
     */
    public function set_address(Request $request,MemberAddressModel $members)
    {
        // 获取用户ID
        $memberId = Session::get('member_id');


        if ($request->isPost()) {

            // 抛出异常
            if (empty($memberId)) throw new Exception('not Empty');

            // 获取参数
            $data = $request->param();
            // 拼接省市区 地区名
            $data['area_info'] = $data['area_info1'] . " " . $data['area_info2'] . " " . $data['area_info3'];
            $data['create_time'] = time();
            $data['member_id'] = $memberId;
            //会员是否含有地址
            $find = $members->where(['member_id' => $memberId])->find();

            if ($find) {

                // 会员有地址
                if ($request->has('status')) {

                    // 设置默认地址
                    $data['status'] = 1;
                    $res_update = $members->where(['member_id' => $memberId, 'status' => 1])->update(['status' => 0]);
                    $res_create = $members->allowField(true)->save($data);
                    return ($res_create && $res_update) ? ['code' => 200] : ['code' => 402, 'message' => '网络繁忙,请重试'];

                } else {

                    // 仅添加地址
                    $data['status'] = 0;
                    $result = $members->allowField(true)->save($data);
                    return $result ? ['code' => 200] : ['code' => 402, 'message' => '网络繁忙,请重试'];
                }
            } else {

                // 会员没有地址
                $data['status'] = 1;
                $result = $members->allowField(true)->save($data);
                return $result ? ['code' => 200, 'message' => '添加成功'] : ['code' => 0, 'message' => '网络繁忙,请重试'];
            }

        }
    }


    /**
     * 删除收货地址
     * @param Request $request
     * @param MemberAddressModel $address
     * @return array
     * @throws Exception
     */
    public function delete_address(Request $request,MemberAddressModel $address)
    {
        // 获取地址ID
        $address_id = $request->param('aid');

        // 抛出异常
        if (empty($address_id)) throw new Exception('not Empty');

        // 删除
        $operation = $address::destroy($address_id);

        if ($operation) {
            return ['code' => 200];
        }
        return ['code' => 402, 'message' => '网络繁忙，请稍后再试'];
    }


    /**
     * 设置默认地址
     * @param Request $request
     * @param MemberAddressModel $model
     * @return array
     */
    public function default_address(Request $request,MemberAddressModel $model)
    {
        // 获取参数
        $id = $request->param('id');
        $memberId = Session::get('member_id');

        // 获取之前的默认地址
        $address = $model->where(['member_id' => $memberId, 'status' => 1])->find();

        if ($address) {
            // 已有默认地址
            $model->save(['status'=>0],['member_id' => $memberId, 'status' => 1]);
            $operation = $model->save(['status' => 1],['id' => $id]);

        }else {
            // 没有默认地址
            $operation = $model->save(['status' => 1],['id' => $id]);
        }

        if ($operation) {
            return ['code' => 200];
        } else {
            return ['code' => 402];
        }


    }


    /**
     * 上传头像
     * @return array
     */
    public function upload_avatar()
    {
        // 获取用户ID
        $memberId = Session::get('member_id');

        if (Request::instance()->isPost()) {
            // 抛出异常
            // if (empty($memberId)) throw new Exception('not Empty');
            // 街参数
            $avatar = Request::instance()->param();

            $operation = (new MemberModel())->allowField(true)->save($avatar, ['id' => $memberId]);

            if ($operation) {
                $find = (new MemberModel())->where('id',$memberId)->value('avatar');
                Session::set('member_avatar',$find);
                return ['code' => 200,'data'=>$find];
            }

            return ['code' => 401, 'message' => '系统服务器繁忙！稍后再试！'];

        }
    }
}
